package com.afu.thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
 * ClassName: ThreadDemo1
 * Package: com.afu.thread
 * Description:
 *              4、线程池实现
 *
 * @Author zxf
 * @Create 2024/2/26 16:14
 * @Version 1.0
 */
public class ThreadPoolDemo4 {

    public static void main(String[] args) {
        try {
//            创建一个可根据需要创建新线程的线程池
            ExecutorService executorService = Executors.newCachedThreadPool();

//            创建一个可重用固定线程数的线程池
            ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);

//            创建一个线程池，它可安排在给定延迟后运行命令或者定期地执行。
            ScheduledExecutorService scheduledThreadPool= Executors.newScheduledThreadPool(3);

//            返回一个线程池（这个线程池只有一个线程）
            ExecutorService singleThreadExecutor= Executors.newSingleThreadExecutor();


            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "在执行。。。  可根据需要创建新线程的线程池");
                }
            });


//            scheduledThreadPool.scheduleWithFixedDelay();
            executorService.shutdown();
            fixedThreadPool.shutdown();
            scheduledThreadPool.shutdown();
            singleThreadExecutor.shutdown();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

